import React from 'react'
import { Table, Input, Button } from 'antd'
import axios from 'axios'
import { checkres } from 'utils'

export default function Log(props) {
  const { num, forwardCfgId, setRowKeys, rowKeys, type, url } = props
  const [data, setData] = React.useState([])
  const [pageIndex, setpageIndex] = React.useState(1)
  const [pageSize, setPageSize] = React.useState(10)
  const [total, setTotal] = React.useState(0)
  const [loading, setLoading] = React.useState(false)
  const [vin, setVin] = React.useState('')

  const columns = [
    {
      title: 'VIN',
      dataIndex: 'vin',
    },
    {
      title: '车牌号',
      dataIndex: 'platenumber',
    },

    {
      title: '车辆型号',
      dataIndex: 'typeName',
    },
  ]

  React.useEffect(() => {
    setpageIndex(1)
  }, [num])

  function getData(params) {
    axios
      .post('forwardcfgvehicle/' + url, {
        ...params,
      })
      .then(res => {
        setLoading(false)
        if (!checkres(res)) return
        setData(res.data.data.records)
        setTotal(res.data.data.total)
      })
  }

  React.useEffect(() => {
    if (!forwardCfgId) return
    if (num !== type) return
    setLoading(true)
    getData({
      pageIndex: pageIndex,
      pageSize: pageSize,
      forwardCfgId: forwardCfgId,
      vin: vin,
    })
  }, [num, pageIndex, pageSize, forwardCfgId])

  return (
    <>
      <Input
        placeholder="请输入vin"
        style={{ width: 200, marginBottom: 24, marginRight: 12 }}
        onChange={e => setVin(e.target.value)}
        value={vin}
      ></Input>
      <Button
        type="primary"
        onClick={() => {
          getData({
            pageIndex: 1,
            pageSize: pageSize,
            forwardCfgId: forwardCfgId,
            vin: vin,
          })
          setpageIndex(1)
        }}
      >
        查询
      </Button>
      <Button
        style={{ marginLeft: 12 }}
        onClick={() => {
          getData({
            pageIndex: 1,
            pageSize: pageSize,
            forwardCfgId: forwardCfgId,
            vin: '',
          })
          setVin('')
          setpageIndex(1)
        }}
      >
        重置
      </Button>
      <Table
        columns={columns}
        dataSource={data}
        loading={loading}
        rowSelection={{
          type: 'checkbox',
          selectedRowKeys: rowKeys,
          onChange: selectRowKeys => {
            setRowKeys(selectRowKeys)
          },
        }}
        rowKey="id"
        pagination={{
          current: pageIndex,
          pageSize: pageSize,
          total: total,
          onChange: (page, pageSize) => {
            setpageIndex(page)
            setPageSize(pageSize)
          },
        }}
      />
    </>
  )
}
